/*
                    FrameGroup Class Implementation
*/
#include "FrameGroup.h"

FrameGroup::FrameGroup()
{
    iFocus = -1;
}

FrameGroup::~FrameGroup()
{
    for (int i = 0; i < Frames.size(); i++)
    {
        delete Frames.at(i);
    }

    Frames.clear();
}

int FrameGroup::parseInput(const SDL_Event &event)
{
    int iReturn = 0;

    switch (event.type)
    {
        case SDL_KEYDOWN:

            iReturn = HandleKeyDown(SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.unicode);

        break;

        case SDL_MOUSEBUTTONDOWN:

            if (event.button.button == SDL_BUTTON_LEFT)
            {
                 HandleMouseDown(event.button.x, event.button.y);
            }

        break;

        case SDL_MOUSEBUTTONUP:

            if (event.button.button == SDL_BUTTON_LEFT)
            {
                 HandleMouseUp(event.button.x, event.button.y);
            }

        break;

        case SDL_MOUSEMOTION:

            HandleMouseMotion(event.motion.xrel, event.motion.yrel);

        break;
    }

    return iReturn;
}

int FrameGroup::HandleMouseDown(int iMouseX, int iMouseY)
{
    if (iFocus != -1)
    {
        if (Frames.at(iFocus)->CheckFocus(iMouseX, iMouseY) == true)
        {
            // Focus doesn't change
        }
        else
        {
            iFocus = -1;

            for (int i = 0; i < Frames.size(); i++)
            {
                if (Frames.at(i)->CheckFocus(iMouseX, iMouseY) == true)
                {
                    iFocus = i;
                }
            }
        }
    }
    else
    {
        iFocus = -1;

        for (int i = 0; i < Frames.size(); i++)
        {
            if (Frames.at(i)->CheckFocus(iMouseX, iMouseY) == true)
            {
                iFocus = i;
            }
        }
    }

    if (iFocus != -1)
    {
        Frames.at(iFocus)->HandleMouseDown(iMouseX, iMouseY);
    }

    return 0;
}

int FrameGroup::HandleMouseUp(int iMouseX, int iMouseY)
{
    if (iFocus != -1)
    {
        Frames.at(iFocus)->HandleMouseUp(iMouseX, iMouseY);
    }

    return 0;
}

int FrameGroup::HandleMouseMotion(int iMouseXREL, int iMouseYREL)
{
    bool bTooltipShown = false;

    for (int i = 0; i < Frames.size(); i++)
    {
        if (Frames.at(i)->HandleMouseMotion(iMouseXREL, iMouseYREL) == 2)
        {
            bTooltipShown = true;
        }
    }

    if (bTooltipShown == false)
    {
        //CursorEngine::HideTooltip();
    }

    return 0;
}

int FrameGroup::HandleWindowResize(int iWindowWidth, int iWindowHeight)
{
    for (int i = 0; i < Frames.size(); i++)
    {
        Frames.at(i)->HandleWindowResize(iWindowWidth, iWindowHeight);
    }

    return 0;
}

int FrameGroup::HandleKeyDown(const string& strKeyName, Uint16 UNICODEvalue)
{
    if (iFocus != -1)
    {
        Frames.at(iFocus)->HandleKeyDown(strKeyName, UNICODEvalue);

        return 1;
    }

    return 0;
}

int FrameGroup::BlitToSurface(SDL_Surface *surface)
{
    for (int i = 0; i < Frames.size(); i++)
    {
        if (i != iFocus)
        {
            Frames.at(i)->BlitToSurface(surface);
        }
    }

    if (iFocus != -1)
    {
        Frames.at(iFocus)->BlitToSurface(surface);
    }

    return 0;
}

int FrameGroup::AddFrame(Frame *frmNew, const string& strName)
{
    Frames.push_back(frmNew);
    Frames.back()->setName(strName);

    return 0;
}

int FrameGroup::GetFrameIndex(const string& strName)
{
    for (int i = 0; i < Frames.size(); i++)
    {
        if (Frames.at(i)->getName() == strName)
        {
            return i;
        }
    }

    return -1;
}

int FrameGroup::SetFrameFocus(const string& strName)
{
    for (int i = 0; i < Frames.size(); i++)
    {
        if (Frames.at(i)->getName() == strName)
        {
            iFocus = i;

            return 0;
        }
    }

    return -1;
}
